﻿Imports System
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.ComponentModel.Design
Imports System.ComponentModel.Design.Serialization
Imports System.IO
Imports System.Workflow.ComponentModel

Namespace System.Workflow.ComponentModel.Design
    Friend MustInherit Class ActivityHostDesigner
        Inherits SequentialActivityDesigner
        ' Methods
        Public Overrides Function CanInsertActivities(ByVal insertLocation As HitTestInfo, ByVal activitiesToInsert As ReadOnlyCollection([Of] Activity)) As Boolean
            Return False
        End Function

        Public Overrides Function CanRemoveActivities(ByVal activitiesToRemove As ReadOnlyCollection([Of] Activity)) As Boolean
            Return False
        End Function

        Protected Overrides Sub Dispose(ByVal disposing As Boolean)
            Try 
                If disposing Then
                    Me.containedRootDesigner = Nothing
                    Me.containedLoader = Nothing
                    If (Not Me.containedDesignSurface Is Nothing) Then
                        Me.containedDesignSurface.Dispose
                        Me.containedDesignSurface = Nothing
                    End If
                    If (Not Me.lastInvokedWorkflowState Is Nothing) Then
                        Me.lastInvokedWorkflowState.Close
                        Me.lastInvokedWorkflowState = Nothing
                    End If
                End If
            Finally
                MyBase.Dispose(disposing)
            End Try
        End Sub

        Public Overrides Sub InsertActivities(ByVal insertLocation As HitTestInfo, ByVal activitiesToInsert As ReadOnlyCollection([Of] Activity))
        End Sub

        Private Function LoadHostedWorkflow() As IWorkflowRootDesigner
            If (Me.RootActivity Is Nothing) Then
                Return Nothing
            End If
            Me.containedLoader = New ContainedDesignerLoader(Me.RootActivity)
            Me.containedDesignSurface = New ContainedDesignSurface(MyBase.Activity.Site, Me)
            If Not Me.containedDesignSurface.IsLoaded Then
                Me.containedDesignSurface.BeginLoad(Me.containedLoader)
            End If
            Return ActivityDesigner.GetSafeRootDesigner([TryCast](Me.containedDesignSurface.GetService(GetType(IDesignerHost)),IServiceProvider))
        End Function

        Protected Overrides Sub LoadViewState(ByVal reader As BinaryReader)
            MyBase.LoadViewState(reader)
            If reader.ReadBoolean Then
                If (Me.containedDesignSurface Is Nothing) Then
                    Me.containedRootDesigner = Me.LoadHostedWorkflow
                End If
                If (Not Me.containedDesignSurface Is Nothing) Then
                    Dim service As IDesignerHost = [TryCast](Me.containedDesignSurface.GetService(GetType(IDesignerHost)),IDesignerHost)
                    If (service Is Nothing) Then
                        Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IDesignerHost).FullName }))
                    End If
                    DesignerHelpers.DeserializeDesignerStates(service, reader)
                End If
            End If
        End Sub

        Protected Sub RefreshHostedActivity()
            If (Not Me.containedRootDesigner Is Nothing) Then
                Me.lastInvokedWorkflowState = New MemoryStream
                Dim writer As New BinaryWriter(Me.lastInvokedWorkflowState)
                Me.SaveViewState(writer)
            End If
            Me.containedRootDesigner = Me.LoadHostedWorkflow
            If (Not Me.lastInvokedWorkflowState Is Nothing) Then
                Me.lastInvokedWorkflowState.Position = 0
                Dim reader As New BinaryReader(Me.lastInvokedWorkflowState)
                Try 
                    Me.LoadViewState(reader)
                Catch obj1 As Object
                End Try
            End If
            MyBase.PerformLayout
        End Sub

        Public Overrides Sub RemoveActivities(ByVal activitiesToRemove As ReadOnlyCollection([Of] Activity))
        End Sub

        Protected Overrides Sub SaveViewState(ByVal writer As BinaryWriter)
            MyBase.SaveViewState(writer)
            If (Not Me.containedDesignSurface Is Nothing) Then
                writer.Write(True)
                Dim service As IDesignerHost = [TryCast](Me.containedDesignSurface.GetService(GetType(IDesignerHost)),IDesignerHost)
                If (service Is Nothing) Then
                    Throw New Exception(SR.GetString("General_MissingService", New Object() { GetType(IDesignerHost).FullName }))
                End If
                DesignerHelpers.SerializeDesignerStates(service, writer)
            Else
                writer.Write(False)
            End If
        End Sub


        ' Properties
        Public Overrides ReadOnly Property ContainedDesigners As ReadOnlyCollection([Of] ActivityDesigner)
            Get
                Dim list As New List([Of] ActivityDesigner)
                If (Not Me.containedRootDesigner Is Nothing) Then
                    list.Add(DirectCast(Me.containedRootDesigner, ActivityDesigner))
                End If
                Return list.AsReadOnly
            End Get
        End Property

        Protected MustOverride ReadOnly Property RootActivity As Activity


        ' Fields
        Private containedDesignSurface As ContainedDesignSurface
        Private containedLoader As ContainedDesignerLoader
        Private containedRootDesigner As IWorkflowRootDesigner
        Private lastInvokedWorkflowState As MemoryStream

        ' Nested Types
        Private NotInheritable Class ContainedDesignerLoader
            Inherits WorkflowDesignerLoader
            ' Methods
            Friend Sub New(ByVal rootActivity As Activity)
                Me.rootActivity = rootActivity
            End Sub

            Public Overrides Sub Flush()
            End Sub

            Public Overrides Sub ForceReload()
            End Sub

            Public Overrides Function GetFileReader(ByVal filePath As String) As TextReader
                Return Nothing
            End Function

            Public Overrides Function GetFileWriter(ByVal filePath As String) As TextWriter
                Return Nothing
            End Function

            Protected Overrides Sub Initialize()
                MyBase.Initialize
                Dim callback As ServiceCreatorCallback = New ServiceCreatorCallback(AddressOf Me.OnCreateService)
                MyBase.LoaderHost.RemoveService(GetType(IReferenceService))
                MyBase.LoaderHost.AddService(GetType(IReferenceService), callback)
            End Sub

            Private Function OnCreateService(ByVal container As IServiceContainer, ByVal serviceType As Type) As Object
                Dim obj2 As Object = Nothing
                If (serviceType Is GetType(IReferenceService)) Then
                    obj2 = New ReferenceService(MyBase.LoaderHost)
                End If
                Return obj2
            End Function

            Protected Overrides Sub PerformLoad(ByVal serializationManager As IDesignerSerializationManager)
                Dim service As IDesignerHost = DirectCast(MyBase.GetService(GetType(IDesignerHost)), IDesignerHost)
                If ((Not Me.rootActivity Is Nothing) AndAlso (Not Me.rootActivity Is Nothing)) Then
                    MyBase.AddActivityToDesigner(Me.rootActivity)
                    MyBase.SetBaseComponentClassName(Me.rootActivity.GetType.FullName)
                End If
            End Sub


            ' Properties
            Public Overrides ReadOnly Property FileName As String
                Get
                    Return String.Empty
                End Get
            End Property


            ' Fields
            Private rootActivity As Activity
        End Class

        Private Class ContainedDesignSurface
            Inherits DesignSurface
            ' Methods
            Friend Sub New(ByVal parentServiceProvider As IServiceProvider, ByVal parentDesigner As CompositeActivityDesigner)
                MyBase.New(parentServiceProvider)
                Me.parentDesigner = parentDesigner
                If (Not MyBase.ServiceContainer Is Nothing) Then
                    MyBase.ServiceContainer.RemoveService(GetType(ISelectionService))
                End If
            End Sub

            Protected Overrides Function CreateDesigner(ByVal component As IComponent, ByVal rootDesigner As Boolean) As IDesigner
                Dim designer As IDesigner = MyBase.CreateDesigner(component, rootDesigner)
                If rootDesigner Then
                    Dim designer2 As IWorkflowRootDesigner = [TryCast](designer,IWorkflowRootDesigner)
                    If (Not designer2 Is Nothing) Then
                        designer2.InvokingDesigner = Me.parentDesigner
                    End If
                End If
                Return designer
            End Function


            ' Fields
            Private parentDesigner As CompositeActivityDesigner
        End Class
    End Class
End Namespace

